package com.hm.hall.commons.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期类
 *
 * @author Luhong
 */
public class FindDate {

    /**
     * 获取每周周一
     *
     * @param i 0为本周周一  1 为上周周一
     * @return
     */
    static public Date findMonday(int i) {
        i = i * 7;
        int mondayPlus;
        Calendar cd = Calendar.getInstance();
        cd.set(Calendar.HOUR_OF_DAY, 0);
        cd.set(Calendar.MINUTE, 0);
        cd.set(Calendar.SECOND, 0);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            mondayPlus = 0;
        } else {
            mondayPlus = 1 - dayOfWeek;
        }
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus - i);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        Date monday = currentDate.getTime();
        return monday;

		/*DateFormat df = DateFormat.getDateInstance();
        String preMonday = df.format(monday);*/

    }

    /**
     * 获取本月第一天00:00:00
     *
     * @return
     */
    static public Date findFirstOfMonth() {
        Date result = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        result = calendar.getTime();
        return result;
    }

    /**
     * 获取本月最后一天最后23:59:59
     *
     * @return
     */
    static public Date findLastOfMonth() {
        Date result = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        result = calendar.getTime();
        return result;
    }

    static public Date findFirstOfDay() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(new Date());
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);

        Date date = new Date(todayStart.getTime().getTime());
        return date;
    }

    static public Date findTodayOfHourAndMinute(int hour, int minute) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(new Date());
        todayStart.set(Calendar.HOUR_OF_DAY, hour);
        todayStart.set(Calendar.MINUTE, minute);
        todayStart.set(Calendar.SECOND, 0);

        Date date = new Date(todayStart.getTime().getTime());
        return date;
    }

    /**
     * 获得本月的剩余时间 (秒)
     *
     * @return 剩余时间 (秒)
     */
    public static long getCurMonthResidueTime() {
        Date monDate = findLastOfMonth();
        return (monDate.getTime() - System.currentTimeMillis()) / 1000;
    }

    /**
     * 获得今天的剩余时间 (毫秒)
     *
     * @return 剩余时间 (毫秒)
     */
    public static long getTodayResidueTime() {
        Calendar c = Calendar.getInstance();
        long now = c.getTimeInMillis();
        c.add(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis() - now;
    }

    /**
     * 获得今天的剩余时间 (秒)
     *
     * @return 剩余时间 (秒)
     */
    public static int getTodayResidueSec() {
        Calendar c = Calendar.getInstance();
        long now = c.getTimeInMillis();
        c.add(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return Integer.valueOf((c.getTimeInMillis() - now) / 1000 + "");
    }

    static public Date getDateByDayMove(Date date, int moveDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, moveDay);
        return calendar.getTime();

    }

    static public Date findHourOfDay(int hour) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(new Date());
        todayStart.set(Calendar.HOUR_OF_DAY, hour);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);

        Date date = new Date(todayStart.getTime().getTime());
        return date;
    }
}
